package ty.tryagainforever1129.pikachumemory.list;
import java.io.IOException;
import java.util.ArrayList;
import ty.tryagainforever1129.pikachumemory.common.Constant;
import ty.tryagainforever1129.pikachumemory.common.Util;
import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class MediaPlayerList {
    ArrayList<MediaPlayer> listMediaPlayer;
    Context context;

    public MediaPlayerList(Context mcontext) {
        this.context = mcontext;
        listMediaPlayer = new ArrayList<MediaPlayer>();
        MediaPlayer media;
        for (int i = 0; i < Constant.MEDIA_PLAYER_LIST_RESULT.length; i++) {
            media = MediaPlayer.create(this.context, Constant.MEDIA_PLAYER_LIST_RESULT[i]);
            listMediaPlayer.add(media);
        }
    }

    public void playMedia(int position) {
        if (Util.getSoundStatus(context) && listMediaPlayer.get(position) != null) {
            if (listMediaPlayer.get(position).isPlaying()) {
                Log.i(Constant.TAG_LOGIC,"reset media: " + position);
                listMediaPlayer.get(position).stop();                
                listMediaPlayer.get(position).reset();
                listMediaPlayer.set(position,MediaPlayer.create(context, Constant.MEDIA_PLAYER_LIST_RESULT[position]));
                try {
                    listMediaPlayer.get(position).prepare();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            listMediaPlayer.get(position).start();
        }
    }

    public void destroy() {
        for (int i = 0; i < listMediaPlayer.size(); i++) {
            if (listMediaPlayer.get(i) != null) {
                listMediaPlayer.get(i).release();
                listMediaPlayer.set(i, null);
            }
        }
    }
}
